from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
import numpy as np
import  tensorflow as  tf

IMG_HEIGHT = 192
IMG_WIDHT = 192
IMG_CHANNEL = 3

# model = tf.keras.models.load_model("../model/cnn.h5")

def app_predict(img_path, model):
    #img_path = '../datasets/flower_photos/sunflowers/40410814_fba3837226_n.jpg'
    img = image.load_img(img_path, target_size=(IMG_HEIGHT, IMG_WIDHT))
    x = image.img_to_array(img)   # (h,w,c)
    x = np.expand_dims(x, axis=0)  # (batch_size, h,w,c)
    x = preprocess_input(x)

    preds = model.predict(x)

    print(preds)
    print(np.argmax(preds))

    dd =  {"苹果": 0, "香蕉": 1, "葡萄": 2, "橙子": 3, "梨子": 4}
    labels = {}
    for  name, index  in  dd.items():
        labels[index] = name

    y =    labels[np.argmax(preds)]
    print(y)
    return y